package com.derbysoft.ota.application.translator.rs;

import com.derbysoft.modules.eip.translator.Translator;
import com.derbysoft.ota.application.util.Calendars;
import com.derbysoft.ota.application.util.ParagraphTypes;
import com.derbysoft.ota.interfaces.protocol._2009a.RateType.Rate;
import com.derbysoft.ota.interfaces.protocol._2009a.TotalType;
import com.derbysoft.remote.dswitch.generic.dto.SimpleAmountDTO;
import com.derbysoft.remote.dswitch.hotel.dto.RateDTO;
import com.derbysoft.remote.share.dto.DateRangeDTO;
import org.apache.commons.lang.StringUtils;

public class RateTranslator implements Translator<RateDTO, Rate> {

	public Rate translate(RateDTO source) {
		Rate rate = new Rate();
		DateRangeDTO dateRange = source.getDateRange();
		if (dateRange != null) {
			rate.setEffectiveDate(Calendars.of(dateRange.getStart()));
			rate.setExpireDate(Calendars.nextDayOf(dateRange.getEnd()));
			rate.setExpireDateExclusiveIndicator(Boolean.TRUE);
		}
		SimpleAmountDTO pureAmount = source.getPureAmount();
		if (pureAmount != null) {
			TotalType base = new TotalType();
			base.setCurrencyCode(pureAmount.getCurrency().getCode());
			base.setAmountBeforeTax(pureAmount.getAmount());
			rate.setBase(base);
		}
		if (StringUtils.isNotBlank(source.getDescription())) {
			rate.setRateDescription(ParagraphTypes.of(source.getDescription()));
		}
		return rate;
	}
}
